import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../store/auth'

// 路由组件按需导入
const Login = () => import('../views/login/Login.vue')
const Register = () => import('../views/login/Register.vue')

// 用户端
const UserLayout = () => import('../layouts/UserLayout.vue')
const UserHome = () => import('../views/user/Home.vue')
const UserProfile = () => import('../views/user/Profile.vue')
const UserProjects = () => import('../views/user/Projects.vue')
const UserPackages = () => import('../views/user/Packages.vue')
const UserOrders = () => import('../views/user/Orders.vue')
const UserResults = () => import('../views/user/Results.vue')

// 医生端
const DoctorLayout = () => import('../layouts/DoctorLayout.vue')
const DoctorHome = () => import('../views/doctor/Home.vue')
const DoctorProfile = () => import('../views/doctor/Profile.vue')
const DoctorSchedule = () => import('../views/doctor/Schedule.vue')
const DoctorOrders = () => import('../views/doctor/Orders.vue')
const DoctorSamples = () => import('../views/doctor/Samples.vue')
const DoctorIndicatorResults = () => import('../views/doctor/IndicatorResults.vue')

// 管理员端
const AdminLayout = () => import('../layouts/AdminLayout.vue')
const AdminHome = () => import('../views/admin/Home.vue')
const AdminProfile = () => import('../views/admin/Profile.vue')
const AdminUsers = () => import('../views/admin/Users.vue')
const AdminDoctors = () => import('../views/admin/Doctors.vue')
const AdminAdmins = () => import('../views/admin/Admins.vue')
const AdminDepartments = () => import('../views/admin/Departments.vue')
const AdminPositions = () => import('../views/admin/Positions.vue')
const AdminIndicatorCategories = () => import('../views/admin/IndicatorCategories.vue')
const AdminIndicators = () => import('../views/admin/Indicators.vue')
const AdminPackages = () => import('../views/admin/Packages.vue')
const AdminOrders = () => import('../views/admin/Orders.vue')
const AdminSchedules = () => import('../views/admin/Schedules.vue')

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    {
      path: '/user',
      component: UserLayout,
      children: [
        { path: 'home', component: UserHome },
        { path: 'profile', component: UserProfile },
        { path: 'projects', component: UserProjects },
        { path: 'packages', component: UserPackages },
        { path: 'orders', component: UserOrders },
        { path: 'results', component: UserResults },
      ]
    },
    {
      path: '/doctor',
      component: DoctorLayout,
      children: [
        { path: 'home', component: DoctorHome },
        { path: 'profile', component: DoctorProfile },
        { path: 'schedule', component: DoctorSchedule },
        { path: 'orders', component: DoctorOrders },
        { path: 'samples', component: DoctorSamples },
        { path: 'indicator-results', component: DoctorIndicatorResults },
      ]
    },
    {
      path: '/admin',
      component: AdminLayout,
      children: [
        { path: 'home', component: AdminHome },
        { path: 'profile', component: AdminProfile },
        { path: 'users', component: AdminUsers },
        { path: 'doctors', component: AdminDoctors },
        { path: 'admins', component: AdminAdmins },
        { path: 'departments', component: AdminDepartments },
        { path: 'positions', component: AdminPositions },
        { path: 'indicator-categories', component: AdminIndicatorCategories },
        { path: 'indicators', component: AdminIndicators },
        { path: 'packages', component: AdminPackages },
        { path: 'orders', component: AdminOrders },
        { path: 'schedules', component: AdminSchedules },
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') return next()
  const auth = useAuthStore()
  if (!auth.isLoggedIn) return next('/login')
  const role = auth.role
  if (to.path.startsWith('/user') && role !== 'user') return next(`/${role}/home`)
  if (to.path.startsWith('/doctor') && role !== 'doctor') return next(`/${role}/home`)
  if (to.path.startsWith('/admin') && role !== 'admin') return next(`/${role}/home`)
  next()
})

export default router